<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

/**
 * Products View
 */
class ProductViewCart extends JView
{
	/**
	 * Cart view display method
	 * @return void
	 */
	function display($tpl = null)
	{
		// Get data from the model
		$items = $this->get('Items');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		
		// Assign data to the view
		$this->items = $items;
		
		$this->total = 0;
		
		if (isset($this->items)) {
			foreach ($this->items as &$item) {
				$item['link'] = JRoute::_('index.php?option=com_product&view=product&id=' . $item['id']);
				$item['thumbnail'] = JURI::root() . 'images/products/' . $item['data']->code . '/thumbnail.jpg';
				$item['deleteLink'] = JRoute::_('index.php?option=com_product&task=cart.delete&id=' . $item['id']);
				$item['price'] = $item['data']->discount > 0 ? $item['data']->discount : $item['data']->price;  
				$item['total'] = $item['price'] * $item['qty'];
				$this->total += $item['total']; 
			}
		}
		
		$this->updateLink = JRoute::_('index.php?option=com_product&task=cart.update');
		$this->checkoutLink = JRoute::_('index.php?option=com_product&view=checkout');
		$this->continueLink = JURI::root();
		
		// Display the template
		parent::display($tpl);
	}
}